<?php

/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/*
 * template_parse_foreach 解析
 * @param $string foreach字符串
 * @return $string
 */

function template_parse_foreach($string) {

    $all_foreach = array();

    preg_match_all("/\{foreach\s([^\{]*?)\}/is", $string, $all_foreach);

    if (count($all_foreach[0]) == 0)
        return $string;

    $replacement = array();

    foreach ($all_foreach[1] as $k => &$v) {
        $temparr = template_k2v($v);
        $tempstring = '';

        empty($temparr['from']) && brk_log(0, get_lang("tpl_syntax") . $all_foreach[0][$k]);
        empty($temparr['item']) && $temparr['item'] = 'item';
        empty($temparr['key']) && $temparr['key'] = 'key';
        empty($temparr['index']) && $temparr['index'] = 'index';

        $temparr['num'] = empty($temparr['num']) ? -1 : intval($temparr['num']);

        $foreach_index = "\$foreach_index__$k";

        $as = " as \$" . $temparr['key'] . "=>\$" . $temparr['item'];

        $index = "$" . $temparr['index'] . "=$foreach_index++-1 ;";

        if ($temparr['num'] < 0) {
            $num = "";
        } else {
            $num = "if($foreach_index>1+" . $temparr['num'] . ") break ;";
        }

        $last = empty($temparr['last']) ? FALSE : "FALSE";
        $first = empty($temparr['first']) ? FALSE : "FALSE";
        if ($last || $first) {
            $count_var = "\$count__$k=count(" . $temparr['from'] . ");";
            $count_if = "\$first=\$last=FALSE; $foreach_index==2 && \$first=TRUE;$foreach_index - 1==" . ($temparr['num'] > 0 ? "min($temparr[num],\$count__$k)" : "\$count__$k" ) . " && \$last=TRUE;";
        } else {
            $count_var = "";
            $count_if = "";
        }

        $foreach_index = $foreach_index . "=1 ;";
        /*
          if (preg_match("/^(\w*)\:(.*)/", $temparr['from'], $funname)) {
          $tempstring.="<?php  \$foreachdata=brkget_" . $funname[1] . "('" . $funname[2] . "')" . "; ";
          $tempstring.="$foreach_index if(!empty(\$foreachdata)){ foreach(\$foreachdata $as) { $num $index?>";
          } else {
         */
        $tempstring.="<?php $foreach_index if(!empty(" . $temparr['from'] . ")){ $count_var foreach(" . $temparr['from'] . " $as){ $index $count_if $num  ?>";
        /*
          }
         */
        $replacement[] = $tempstring;
    }

    template_arr2patt($all_foreach[0]);
    $string = preg_replace($all_foreach[0], $replacement, $string, 1);
    $string = preg_replace("/\{\/foreach\}/", "<?php }} ?>", $string);
    $string = preg_replace("/\{foreachelse\}/", "<?php else{ ?>", $string);
    $string = preg_replace("/\{\/foreachelse\}/", "<?php } ?>", $string);

    return $string;
}
?>